﻿namespace AbpPlus
{
	using System.Collections.Generic;

	using Abp.Dependency;
	using Abp.Modules;
	using Abp.Reflection.Extensions;

	using AbpPlus.Bus;
	using AbpPlus.Extentions;

	using Castle.MicroKernel.Registration;

	using MediatR;

	/// <summary>
	/// cqrs 模块.
	/// </summary>
	public class AbpCqrsModule : AbpModule
	{
		/// <summary>
		/// The pre initialize.
		/// </summary>
		public override void PreInitialize()
		{
			this.IocManager.Register<IMediatorHandler, InMemoryBus>(
				DependencyLifeStyle.Transient);
			this.IocManager.Register<IMediator, Mediator>(
				DependencyLifeStyle.Transient);

			var container = this.IocManager.IocContainer;
			
			container.Register(Component.For<SingleInstanceFactory>().UsingFactoryMethod<SingleInstanceFactory>(k => k.Resolve));
			container.Register(Component.For<MultiInstanceFactory>().UsingFactoryMethod<MultiInstanceFactory>(k => t => (IEnumerable<object>)k.ResolveAll(t)));
		}

		/// <summary>
		/// 初始化
		/// </summary>
		public override void Initialize()
		{
			var assembly = typeof(AbpCqrsModule).GetAssembly();
			this.IocManager.RegisterAssemblyHandler(assembly);
			this.IocManager.RegisterAssemblyByConvention(assembly);
		}
	}
}